package com.faya.green.service.impl;

import com.faya.green.abnormal.ServiceException;
import com.faya.green.common.Constant;
import com.faya.green.dao.user.ResumeDao;
import com.faya.green.dao.user.UserDao;
import com.faya.green.enumeration.StatusEnum;
import com.faya.green.model.Resume;
import com.faya.green.model.User;
import com.faya.green.service.ResumeService;
import com.faya.green.utils.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author ：LX
 * 创建时间： 2018/8/24. 23:13
 * 地点：广州
 * 目的: 履历信息
 * 备注说明：
 */
@Service
public class ResumeServiceImpl implements ResumeService {

    //压制全部警告
    @SuppressWarnings("all")
    @Autowired
    private ResumeDao resumeDao;
    //压制全部警告
    @SuppressWarnings("all")
    @Autowired
    private UserDao userDao;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addResume(Resume resume) {
        if (CommonUtils.checkNull(resume.getUserId())){
            throw new ServiceException("用户ID找不到");
        }
        User user = userDao.findUserById(resume.getUserId());
        if (user == null){
            throw new ServiceException("找不到用户");
        }
        user.setIsResume(Constant.YES);
        userDao.updateUser(user);

        resume.setStatus(StatusEnum.STATUS_ADD.getCode());
        resume.setId(UUID.randomUUID().toString());
        resume.setCreateTime(new Date());
        resume.setUpdateTime(new Date());

        resumeDao.addResume(resume);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateResume(Resume resume) {
        if (CommonUtils.checkNull(resume.getUserId())){
            throw new ServiceException("用户ID找不到");
        }
        resume.setStatus(StatusEnum.STATUS_UPDATE.getCode());
        resume.setUpdateTime(new Date());

        resumeDao.updateResume(resume);
    }

    @Override
    public Resume getResumeById(String id) {
        return resumeDao.getResumeById(id);
    }

    @Override
    public List<Resume> getResumeByUserIdPubilc(String userId, String isPubilc) {
        return resumeDao.getResumeByUserIdPubilc(userId, isPubilc);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delResume(String id) {
        Resume resume = resumeDao.getResumeById(id);
        if (resume != null){
            String userId = resume.getUserId();
            List<Resume> resumeList = resumeDao.getResumeByUserIdPubilc(userId, null);
            if (resumeList.size() == 0 || resumeList.size() == 1){
                User user = userDao.findUserById(userId);
                user.setIsResume(Constant.NO);
                userDao.updateUser(user);
            }
            resumeDao.delResume(id);
        }
    }
}
